﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Rainbow.common.validation
{
    public class FileNameRule : ValidationRule
    {

        public string errString { get; set; }


        public FileNameRule()
        {

        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            bool rn = true;

            try
            {
                if (string.IsNullOrEmpty((string)value))
                {
                    return new ValidationResult(false, $"不能为空");
                }

                char[] invalidFileNameChars = System.IO.Path.GetInvalidFileNameChars();
                if (((string)value).IndexOfAny(invalidFileNameChars) != -1)
                {
                    rn = false;
                }

            }
            catch (Exception e)
            {
                return new ValidationResult(false, $"输入字符不合法或者{e.Message}");
            }

            if (rn == false)
            {
                return new ValidationResult(false, errString);
            }

            return ValidationResult.ValidResult;
        }
    }
}
